#include "RingQueue.hpp"
#include "Task.hpp"
#include <pthread.h>
#include <unistd.h>
#include <iostream>

void* produce(void* args)
{
    RingQueue<Task>* rq = static_cast<RingQueue<Task>*>(args);
    std::string opstr = "+-*/%";
    while(true)
    {
        int x = rand() % 100;
        int y = rand() % 10;
        int opcode = rand() % opstr.size();
        char op = opstr[opcode];
        Task t(x,y,op,mymath);
        rq->Push(t);
        std::cout << "生产任务：" << t.toString() << std::endl;
        sleep(1);
    }


    return nullptr;
}

void* comsume(void* args)
{
    RingQueue<Task>* rq = static_cast<RingQueue<Task>*>(args);
    while(true)
    {
        Task t;
        rq->Pop(&t);
        std::cout << "消费任务：" << t() << std::endl;
    }

    return nullptr;
}


int main()
{
    RingQueue<Task>* rq = new RingQueue<Task>;

    const int cnum = 5;
    const int pnum = 10;
    pthread_t producer[pnum], comsumer[cnum];
    for(int i = 0; i < pnum; i++)
    {
        pthread_create(producer+i, nullptr, produce, (void*)rq);
    }

    for(int i = 0; i < cnum; i++)
    {
        pthread_create(comsumer + i, nullptr, comsume, (void*)rq);
    }

    for(int i = 0; i < pnum; i++)
    {
        pthread_join(producer[i], nullptr);
    }
    for(int i = 0; i < cnum; i++)
    {
        pthread_join(comsumer[i], nullptr);
    }

    delete rq;



    return 0;
}